package com.springsummer.information.green.beans;





import com.springsummer.information.green.annotation.AutoValue;
import com.springsummer.information.green.annotation.Autowired;

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

public class InjectionElement {

    protected Member member;

    protected boolean fieldFlag;

    protected boolean methodFlag;

    protected String annotationName;

    protected Autowired autowiredMeta;

    protected AutoValue autoValueMeta;

    public InjectionElement() {
    }

    public InjectionElement(Member member, boolean fieldFlag) {
        this.member = member;
        this.fieldFlag = fieldFlag;
    }

    public InjectionElement(Member member, boolean fieldFlag, boolean methodFlag, String annotationName) {
        this.member = member;
        this.fieldFlag = fieldFlag;
        this.methodFlag = methodFlag;
        this.annotationName = annotationName;
    }

    public Member getMember() {
        return member;
    }

    public String getMemberName(){
        if(member==null){
            return null;
        }
        return member.getName();
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public boolean isFieldFlag() {
        return fieldFlag;
    }

    public void setFieldFlag(boolean fieldFlag) {
        this.fieldFlag = fieldFlag;
    }

    public boolean isMethodFlag() {
        return methodFlag;
    }

    public void setMethodFlag(boolean methodFlag) {
        this.methodFlag = methodFlag;
    }

    public String getAnnotationName() {
        return annotationName;
    }

    public void setAnnotationName(String annotationName) {
        this.annotationName = annotationName;
    }

    public Autowired getAutowiredMeta() {
        return autowiredMeta;
    }

    public void setAutowiredMeta(Autowired autowiredMeta) {
        this.autowiredMeta = autowiredMeta;
    }

    public AutoValue getAutoValueMeta() {
        return autoValueMeta;
    }

    public void setAutoValueMeta(AutoValue autoValueMeta) {
        this.autoValueMeta = autoValueMeta;
    }

    public boolean autowireValueFlag(){
        return AutoValue.class.getSimpleName().equalsIgnoreCase(annotationName);
    }

    public boolean autowireBeanFlag(){
        return Autowired.class.getSimpleName().equalsIgnoreCase(annotationName);
    }

    public void inject(Object target, String beanName, Object memberValue)
            throws Exception {

        if (this.fieldFlag) {
            Field field = (Field) this.member;
            if(!field.canAccess(target)){field.setAccessible(true);}
            field.set(target, memberValue);
        }
        else {

            try {
                Method method = (Method) this.member;
                if(!method.isAccessible()){method.setAccessible(true);}
                method.invoke(target, memberValue);
            }
            catch (Exception ex) {
                throw ex;
            }
        }
    }

}
